
local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ConnectClass = _G.CommonOHOH.load.Connect();
local EventList = ServerOHOH.load.EventList();
local PetTool=_G.CommonOHOH.load.PetTool();

function M.new(self,...)
	return M(self,...);
end;
function M.ctor(self, parent)

	M.super.ctor(self, parent);

	self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
    local f= Instance.new("Folder",self.parent.main_inst)
    f.Name="Trade"
    local tradeSetting=Instance.new("IntValue",f)-- 0：关闭交易，1：所有交易，2：好友交易
    tradeSetting.Value=self.DataBase:GetData("set_data").trade
    tradeSetting.Name="tradeSetting"

    local tradeSate=Instance.new("IntValue",f) -- 0，无交易，1：交易中
    tradeSate.Name="tradeSate"

    self.tradeSetting=tradeSetting
    self.tradeSate=tradeSate
    self.ChangeAction=ConnectClass.new()
    self.ReadyAction=ConnectClass.new()
    self.CancelAction=ConnectClass.new()
    self.DtorAction=ConnectClass.new()
    self.Asking={}
    self.pets={}

end;

function M.dtor(self)
    self.DtorAction:Action()
	M.super.dtor(self);
end;

function M.IsCanSendAsk(self,player)
    local otherUserId=player:GetUserId()
    if  self.Asking[otherUserId] then
        return false
    end
    self.Asking[otherUserId]=true
    self:AddProcess(function()
        self:Wait(8) --8秒后允许再次发送
        self.Asking[otherUserId]=nil
    end)

    return self:IsCanTrade(player)
end

function M.IsCanTrade(self,player)
    assert(player,"IsCanTrade-->player==nil")
    if  self.tradeSate.Value==1 then --交易中
         return false
    end

   if  self.tradeSetting.Value==0 then --已经关闭交易
        return false
   end

   --非好友不能交易
   if  self.tradeSetting.Value==2 and (not self.parent.main_inst:IsFriendsWith(player:GetUserId())) then
        return  false
   end

   return true

end

--0，关闭交易，1和所有人交易，2，仅和好友交易
function M.SetState(self,isOpen)
    -- if value==1 then --所有
    --     if  self.tradeSetting.Value==1 then --原来是所有
    --         self.tradeSetting.Value=2 
    --     elseif  self.tradeSetting.Value==2 then --原来是好友
    --         self.tradeSetting.Value=1
    --     elseif  self.tradeSetting.Value==0 then --原来是关闭
    --         self.tradeSetting.Value=1
    --     end
    -- elseif value==2 then --好友 

    --     if  self.tradeSetting.Value==1 then 
    --         self.tradeSetting.Value=0
    --     elseif  self.tradeSetting.Value==2 then
    --         self.tradeSetting.Value=0
    --     elseif  self.tradeSetting.Value==0 then
    --         self.tradeSetting.Value=2
    --     end
    -- end

    if isOpen then
        self.tradeSetting.Value=1
    else
        self.tradeSetting.Value=0
    end

    self.DataBase:GetData("set_data").trade= self.tradeSetting.Value

end
function M.PetDataFix(self,data)
    local new={}
    for k,v in pairs(data) do
        if self.DataBase:GetData("pets").had[k] and self.DataBase:GetData("pets").had[k]>=v then
            new[k]=v
        end
    end
   
    return new
end

function M.GetAllPet(self)
    -- warn("-----GetAllPet-------------",self.DataBase:GetData("pets").had.__real)
    local data=self.DataBase:GetData("pets").had.__real
    -- for k,v in pairs(data) do
    --     print(k,v)
    -- end

    return data
end
--开始交易
function M.StartTrade(self,userId,tid,had)
    -- warn("-------StartTrade-----------",userId,tid,had)
    self.ReadyFlish=nil
    self.pets={}
    self.tradeSate.Value=1
    self.parent:Fire(EventList.Trading,userId,tid,had)
end
--添加宠物
function M.AddPet(self,data)
    --检查是否存在Pet-->data
    local data=self:PetDataFix(data)
    for id,count in pairs(data) do
        if self.pets[id] then
            self.pets[id]=self.pets[id]+count
        else
            self.pets[id]=count
        end
    end
end

--移除宠物
function M.RemovePet(self,data)
    for id,count in pairs(data) do
        assert(self.pets[id],"self.pets[id]==nil"..id)
        self.pets[id]=self.pets[id]-count
    end
end

--准备
function M.Ready(self)
    self.ReadyAction:Action(self.pets)
end

--取消准备
function M.Cancel(self)
    self.ReadyFlish=nil
    self.tradeSate.Value=0
    self.pets={}
    self.CancelAction:Action(self.pets)
end
function M.TargetCancel(self,userId,tid)
    self.tradeSate.Value=0
    self:Fire(EventList.target_cancel,userId,tid)
end

-- 发送对方交易信息更改
function M.UpdateTargetPet(self,pets)
    self:Fire(EventList.targetPet_upate,pets)
end

-- 对方已准备
function M.TargetReady(self)
    self:Fire(EventList.target_ready)
end
--改变交易内容
function M.Change(self,pets,op)
    assert(pets,"pets==null")
    assert(op,"op==null")
    if self.ReadyFlish then
        return
    end
    if op==1 then --增加
       self:AddPet(pets)
    else-- 移除
        self:RemovePet(pets)
    end
    self.ChangeAction:Action(self.pets)
end
function M.GetTradePet(self)
    return self.pets
end


function M.ReadyFinish(self)
    self.ReadyFlish=true
    self:Fire(EventList.ready_finish)
end

function M.Finish(self,otherPets)
    self.ReadyFlish=nil
    self.tradeSate.Value=0
    --移除Pets
    local new={}
    local RemovePetIds={}
    for k,v in pairs(self.pets) do
        if v<=0 then
            continue
        end

        if self.DataBase:GetData("pets").had[k] and self.DataBase:GetData("pets").had[k]>=v then
            table.insert(RemovePetIds,k)
            self.DataBase:GetData("pets").had[k]=self.DataBase:GetData("pets").had[k]-v
            if self.DataBase:GetData("pets").had[k]==0 then
                self.DataBase:GetData("pets").had[k]=nil
            end
        end
    end

     --添加pets
    for k,v in pairs(otherPets) do
        if v<=0 then
            continue
        end
        if self.DataBase:GetData("pets").had[k] then
            self.DataBase:GetData("pets").had[k]=self.DataBase:GetData("pets").had[k]+v 
        else
            self.DataBase:GetData("pets").had[k]=v
        end
    end

    for _,v in pairs(RemovePetIds) do
        PetTool.UpdateEquip(v,self.DataBase:GetData("pets").had,self.DataBase:GetData("pets").equip)
    end

    self.parent.PetEuqipUpdateEvent:Action()

    --存储
  	 self.DataBase:Execut(function()
	 end);



    
    self.parent:SynchDataToClient("pets")
end

M:RegistEvent(EventList.set_trade,M.SetState)
M:RegistEvent(EventList.trade_ready,M.Ready)
M:RegistEvent(EventList.target_cancel,M.Cancel)
M:RegistEvent(EventList.trade_change,M.Change)

return M